home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / setup / vbnet / 09 array, lists, and collections / collectionsdemo / classes.vb < prev    next >
Encoding:
Text File  |  2002-03-16  |  3.8 KB  |  137 lines

  1. ' this structure is used to demonstrate the Array.Sort method
  2.  
  3. Structure Employee
  4.     Public FirstName As String
  5.     Public LastName As String
  6.     Public HireDate As Date
  7.  
  8.     Sub New(ByVal FirstName As String, ByVal LastName As String, _
  9.         ByVal HireDate As Date)
  10.         Me.FirstName = FirstName
  11.         Me.LastName = LastName
  12.         Me.HireDate = HireDate
  13.     End Sub
  14.  
  15.     ' A function to display an element easily
  16.     Function Description() As String
  17.         Return FirstName & " " & LastName & _
  18.             " (hired on " & HireDate.ToShortDateString & ")"
  19.     End Function
  20. End Structure
  21.  
  22. ' this class is used to sort a SortedList in reverse order
  23.  
  24. Class ReverseStringComparer
  25.     Implements IComparer
  26.  
  27.     Function CompareValues(ByVal x As Object, ByVal y As Object) As Integer _
  28.         Implements IComparer.Compare
  29.         ' Just change the sign of the result of the StrComp.
  30.         Return -StrComp(x.ToString, y.ToString)
  31.     End Function
  32. End Class
  33.  
  34. ' a collection class with fixed membership
  35.  
  36. Class PowersOfTwoCollection
  37.     Inherits System.Collections.ReadOnlyCollectionBase
  38.  
  39.     Sub New(ByVal MaxExponent As Integer)
  40.         MyBase.New()
  41.  
  42.         ' Fill the inner ArrayList object.        
  43.         Dim index As Integer
  44.         For Index = 0 To MaxExponent
  45.             InnerList.Add(2 ^ Index)
  46.         Next
  47.     End Sub
  48.  
  49.     ' Support for the Item element (read-only).
  50.     Default ReadOnly Property Item(ByVal Exponent As Integer) As Long
  51.         Get
  52.             Return CLng(InnerList.Item(Exponent))
  53.         End Get
  54.     End Property
  55. End Class
  56.  
  57. 'a class used to demonstrate the SquareCollection class
  58.  
  59. Class Square
  60.     Public Side As Single
  61.  
  62.     ' A simple constructor
  63.     Sub New(ByVal side As Single)
  64.         Me.Side = side
  65.     End Sub
  66. End Class
  67.  
  68. ' A collection object that can only store Square objects.
  69.  
  70. Class SquareCollection
  71.     Inherits System.Collections.CollectionBase
  72.  
  73.     ' You can add only Square objects to this collection.    
  74.     Sub Add(ByVal value As Square)
  75.         InnerList.Add(value)
  76.         ' keep the total area updated
  77.         m_TotalArea += (value.Side * value.Side)
  78.     End Sub
  79.  
  80.     ' The Item property sets or returns a Square object.
  81.     Default Property Item(ByVal index As Integer) As Square
  82.         Get
  83.             Return CType(InnerList.Item(index), Square)
  84.         End Get
  85.         Set(ByVal Value As Square)
  86.             InnerList.Item(index) = Value
  87.         End Set
  88.     End Property
  89.  
  90.     ' keep track of the total area of squares
  91.     Dim m_TotalArea As Single
  92.  
  93.     ReadOnly Property TotalArea() As Single
  94.         Get
  95.             Return m_TotalArea
  96.         End Get
  97.     End Property
  98.  
  99.     Function Create(ByVal Side As Single) As Square
  100.         Create = New Square(Side)
  101.         Add(Create)
  102.     End Function
  103.  
  104.     Protected Overrides Sub OnRemoveComplete(ByVal index As Integer, ByVal value As Object)
  105.         ' get a reference to the square being removed
  106.         Dim sq As Square = CType(value, Square)
  107.         ' keep the total area updated.
  108.         m_TotalArea -= (sq.Side * sq.Side)
  109.     End Sub
  110. End Class
  111.  
  112. ' a class that inherits from DictionaryBase
  113.  
  114. Class SquareDictionary
  115.     Inherits System.Collections.DictionaryBase
  116.  
  117.     Sub Add(ByVal Key As String, ByVal Value As Square)
  118.         Dictionary.Add(Key, Value)
  119.     End Sub
  120.  
  121.     Function Create(ByVal Key As String, ByVal Side As Single) As Square
  122.         Create = New Square(Side)
  123.         ' Note that we use the function name as a local variable.
  124.         Dictionary.Add(Key, Create)
  125.     End Function
  126.  
  127.     Default Property Item(ByVal Key As String) As Square
  128.         Get
  129.             Return CType(Dictionary.Item(Key), Square)
  130.         End Get
  131.         Set(ByVal Value As Square)
  132.             Dictionary.Item(Key) = Value
  133.         End Set
  134.     End Property
  135. End Class
  136.  
  137.